package com.heeexy.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.service.DBService;
import com.heeexy.example.util.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/db")
public class DBController {

    @Autowired
    private DBService dbService;


    @GetMapping("/backup")
    public JSONObject dbbackup(HttpServletRequest request) {
        String filePath="D:\\";
        String dbName="example";
        try{
            Process process = Runtime.getRuntime().exec(
                    "cmd  /c  mysqldump -u root -proot " + dbName + " > "
                            + filePath + "/" + dbName+new java.util.Date().getTime()
                            + ".sql");
            return dbService.DBbackup(CommonUtil.request2Json(request));
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/listDB")
    public JSONObject listDB(HttpServletRequest request){
        return dbService.listDB(CommonUtil.request2Json(request));
    }

    @PostMapping("/addDB")
    public JSONObject addDB(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "Size");
        return dbService.addDB(requestJson);
    }

    @PostMapping("/deleteDB")
    public JSONObject deleteDB(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson, "Id");
        return dbService.deleteDB(requestJson);
    }

}
